# Basic axis-aligned bounding boxes are just pairs of triples of coordinates,
# providing xmin and xmax.
# Robert Bridson
# Released under the GPLv3

import math

def bBox(p, q):
    """
    Returns minimal bounding box containing the two 3D points p and q.
    """
    return (min(p[0],q[0]), min(p[1],q[1]), min(p[2],q[2])), \
           (max(p[0],q[0]), max(p[1],q[1]), max(p[2],q[2]))

def united(box1, box2):
    """
    Returns the minimal box containing the two input bounding boxes.
    """
    return (min(box1[0][0],box2[0][0]), \
            min(box1[0][1],box2[0][1]), \
            min(box1[0][2],box2[0][2])), \
           (max(box1[1][0],box2[1][0]), \
            max(box1[1][1],box2[1][1]), \
            max(box1[1][2],box2[1][2]))

def sphere(box):
    c = (0.5*(box[0][0]+box[1][0]),
         0.5*(box[0][1]+box[1][1]),
         0.5*(box[0][2]+box[1][2]))
    r = math.sqrt((c[0]-box[0][0])**2 + \
                  (c[1]-box[0][1])**2 + \
                  (c[2]-box[0][2])**2)
    return c, r

